package com.wangsd.api.controller;

import com.wangsd.common.controller.BaseController;
import com.wangsd.common.exception.BusinessErrorEnum;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.common.jwt.LoginUser;
import com.wangsd.common.jwt.PassToken;
import com.wangsd.common.jwt.TokenService;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.member.entity.Member;
import com.wangsd.member.entity.MemberCoupon;
import com.wangsd.member.service.MemberCouponService;
import com.wangsd.member.service.MemberService;
import com.wangsd.system.entity.Coupon;
import com.wangsd.system.entity.Merchant;
import com.wangsd.system.entity.Trade;
import com.wangsd.system.service.MerchantService;
import com.wangsd.system.service.TradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/checkoutCounter")
public class CheckoutCounter extends BaseController {
    @Value("${wx-auth-notify}")
    private String notify_url;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberCouponService memberCouponService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TradeService tradeService;

    @RequestMapping("/index")
    @PassToken
    public String index(Model model, String openid,Long state){
        log.info("openId:"+openid);
        Member member = memberService.selectMemberByOpenid(openid);
        if (member != null) {
            MemberCoupon memberCoupon = new MemberCoupon();
            memberCoupon.setMemberId(member.getId());
            memberCoupon.setMerchantId(state);
            memberCoupon.setStatus(1);
            List<Coupon> coupons = memberCouponService.selectMemberCouponList(memberCoupon);
            model.addAttribute("coupons",coupons);
        }
        if (state != null) {
            Merchant merchant = merchantService.selectMerchantById(state);
            model.addAttribute("merchant",merchant);
        }
        LoginUser loginUser = LoginUser.builder().miniOpenid(openid).member(member).build();
        String token = tokenService.createToken(loginUser);
        model.addAttribute("member",member);
        model.addAttribute("token",token);
        return "checkoutCounter";
    }

    /**
     * 获取用户openid
     * (二维码地址)http://coupon.alipayjf.com/api/h5/getWxOpenid?wxappid=wx29d91fb23adf83c8&merchantId=1
     * @param wxappid
     */
    @PassToken
    @GetMapping("/getWxOpenid")
    @ResponseBody
    public void getWxOpenid(String wxappid, Long merchantId) {
        if (StringUtils.isBlank(wxappid)) {
            throw new BusinessException(BusinessErrorEnum.UNKNOWN_ERROR,"没有获取到appid");
        }
        String callBack = notify_url + "/checkoutCounter/index";
        String redirect = "https://wechat.alipayjf.com/api/open/authorize?appid=" + wxappid
                + "&from=" + callBack + "&state=" + merchantId;
        try {
            response.sendRedirect(redirect);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/paySuccess")
    @PassToken
    public String paySuccess(Model model,String out_trade_no){
        Trade trade = tradeService.selectTradeByTradeNo(out_trade_no);
        model.addAttribute("trade",trade);
        return "afterPayment";
    }
}
